home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Mac / Modules / macmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-23  |  11.3 KB  |  638 lines  |  [TEXT/CWIE]

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Mac module implementation */
  26.  
  27. #include "allobjects.h"
  28. #include "modsupport.h"
  29. #include "ceval.h"
  30.  
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <errno.h>
  34.  
  35. #ifdef THINK_C
  36. #include "unix.h"
  37. #undef S_IFMT
  38. #undef S_IFDIR
  39. #undef S_IFCHR
  40. #undef S_IFBLK
  41. #undef S_IFREG
  42. #undef S_ISDIR
  43. #undef S_ISREG
  44. #endif
  45.  
  46. #include "macstat.h"
  47. #ifdef USE_GUSI
  48. /* Remove defines from macstat.h */
  49. #undef S_IFMT
  50. #undef S_IFDIR
  51. #undef S_IFREG
  52. #undef S_IREAD
  53. #undef S_IWRITE
  54. #undef S_IEXEC
  55.  
  56. #include <GUSI.h>
  57. #include <sys/types.h>
  58. #include <stat.h>
  59. #else
  60. #define stat macstat
  61. #endif
  62.  
  63. #ifdef __MWERKS__
  64. #include <unix.h>
  65. #else
  66. #include <fcntl.h>
  67. #endif
  68.  
  69. /* Optional routines, for some compiler/runtime combinations */
  70. #if defined(USE_GUSI) || !defined(__MWERKS__)
  71. #define WEHAVE_FDOPEN
  72. #endif
  73. #if defined(MPW) || defined(USE_GUSI)
  74. #define WEHAVE_DUP
  75. #endif
  76.  
  77. #include "macdefs.h"
  78. #ifdef USE_GUSI
  79. #include <dirent.h>
  80. #else
  81. #include "dirent.h"
  82. #endif
  83.  
  84. #ifndef MAXPATHLEN
  85. #define MAXPATHLEN 1024
  86. #endif
  87.  
  88. /* Prototypes for Unix simulation on Mac */
  89.  
  90. #ifndef USE_GUSI
  91.  
  92. int chdir PROTO((const char *path));
  93. int mkdir PROTO((const char *path, int mode));
  94. DIR * opendir PROTO((char *));
  95. void closedir PROTO((DIR *));
  96. struct dirent * readdir PROTO((DIR *));
  97. int rmdir PROTO((const char *path));
  98. int sync PROTO((void));
  99.  
  100. #if defined(THINK_C) || defined(__SC__)
  101. int unlink PROTO((char *));
  102. #else
  103. int unlink PROTO((const char *));
  104. #endif
  105.  
  106. #endif /* USE_GUSI */
  107.  
  108. char *getwd PROTO((char *));
  109. char *getbootvol PROTO((void));
  110.  
  111.  
  112. static object *MacError; /* Exception mac.error */
  113.  
  114. /* Set a MAC-specific error from errno, and return NULL */
  115.  
  116. static object * 
  117. mac_error() 
  118. {
  119.     return err_errno(MacError);
  120. }
  121.  
  122. /* MAC generic methods */
  123.  
  124. static object *
  125. mac_1str(args, func)
  126.     object *args;
  127.     int (*func) FPROTO((const char *));
  128. {
  129.     char *path1;
  130.     int res;
  131.     if (!getargs(args, "s", &path1))
  132.         return NULL;
  133.     BGN_SAVE
  134.     res = (*func)(path1);
  135.     END_SAVE
  136.     if (res < 0)
  137.         return mac_error();
  138.     INCREF(None);
  139.     return None;
  140. }
  141.  
  142. static object *
  143. mac_2str(args, func)
  144.     object *args;
  145.     int (*func) FPROTO((const char *, const char *));
  146. {
  147.     char *path1, *path2;
  148.     int res;
  149.     if (!getargs(args, "(ss)", &path1, &path2))
  150.         return NULL;
  151.     BGN_SAVE
  152.     res = (*func)(path1, path2);
  153.     END_SAVE
  154.     if (res < 0)
  155.         return mac_error();
  156.     INCREF(None);
  157.     return None;
  158. }
  159.  
  160. static object *
  161. mac_strint(args, func)
  162.     object *args;
  163.     int (*func) FPROTO((const char *, int));
  164. {
  165.     char *path;
  166.     int i;
  167.     int res;
  168.     if (!getargs(args, "(si)", &path, &i))
  169.         return NULL;
  170.     BGN_SAVE
  171.     res = (*func)(path, i);
  172.     END_SAVE
  173.     if (res < 0)
  174.         return mac_error();
  175.     INCREF(None);
  176.     return None;
  177. }
  178.  
  179. static object *
  180. mac_chdir(self, args)
  181.     object *self;
  182.     object *args;
  183. {
  184. #ifdef USE_GUSI
  185.     object *rv;
  186.     
  187.     /* Change MacOS's idea of wd too */
  188.     rv = mac_1str(args, chdir);
  189.     PyMac_FixGUSIcd();
  190.     return rv;
  191. #else
  192.     return mac_1str(args, chdir);
  193. #endif
  194.  
  195. }
  196.  
  197. static object *
  198. mac_close(self, args)
  199.     object *self;
  200.     object *args;
  201. {
  202.     int fd, res;
  203.     if (!getargs(args, "i", &fd))
  204.         return NULL;
  205.     BGN_SAVE
  206.     res = close(fd);
  207.     END_SAVE
  208. #ifndef USE_GUSI
  209.     /* GUSI gives surious errors here? */
  210.     if (res < 0)
  211.         return mac_error();
  212. #endif
  213.     INCREF(None);
  214.     return None;
  215. }
  216.  
  217. #ifdef WEHAVE_DUP
  218.  
  219. static object *
  220. mac_dup(self, args)
  221.     object *self;
  222.     object *args;
  223. {
  224.     int fd;
  225.     if (!getargs(args, "i", &fd))
  226.         return NULL;
  227.     BGN_SAVE
  228.     fd = dup(fd);
  229.     END_SAVE
  230.     if (fd < 0)
  231.         return mac_error();
  232.     return newintobject((long)fd);
  233. }
  234.  
  235. #endif
  236.  
  237. #ifdef WEHAVE_FDOPEN
  238. static object *
  239. mac_fdopen(self, args)
  240.     object *self;
  241.     object *args;
  242. {
  243.     extern int fclose PROTO((FILE *));
  244.     int fd;
  245.     char *mode;
  246.     FILE *fp;
  247.     if (!getargs(args, "(is)", &fd, &mode))
  248.         return NULL;
  249.     BGN_SAVE
  250.     fp = fdopen(fd, mode);
  251.     END_SAVE
  252.     if (fp == NULL)
  253.         return mac_error();
  254.     return newopenfileobject(fp, "(fdopen)", mode, fclose);
  255. }
  256. #endif
  257.  
  258. static object *
  259. mac_getbootvol(self, args)
  260.     object *self;
  261.     object *args;
  262. {
  263.     char *res;
  264.     if (!getnoarg(args))
  265.         return NULL;
  266.     BGN_SAVE
  267.     res = getbootvol();
  268.     END_SAVE
  269.     if (res == NULL)
  270.         return mac_error();
  271.     return newstringobject(res);
  272. }
  273.  
  274. static object *
  275. mac_getcwd(self, args)
  276.     object *self;
  277.     object *args;
  278. {
  279.     char path[MAXPATHLEN];
  280.     char *res;
  281.     if (!getnoarg(args))
  282.         return NULL;
  283.     BGN_SAVE
  284. #ifdef USE_GUSI
  285.     res = getcwd(path, sizeof path);
  286. #else
  287.     res = getwd(path);
  288. #endif
  289.     END_SAVE
  290.     if (res == NULL) {
  291.         err_setstr(MacError, path);
  292.         return NULL;
  293.     }
  294.     return newstringobject(res);
  295. }
  296.  
  297. static object *
  298. mac_listdir(self, args)
  299.     object *self;
  300.     object *args;
  301. {
  302.     char *name;
  303.     object *d, *v;
  304.     DIR *dirp;
  305.     struct dirent *ep;
  306.     if (!getargs(args, "s", &name))
  307.         return NULL;
  308.     BGN_SAVE
  309.     if ((dirp = opendir(name)) == NULL) {
  310.         RET_SAVE
  311.         return mac_error();
  312.     }
  313.     if ((d = newlistobject(0)) == NULL) {
  314.         closedir(dirp);
  315.         RET_SAVE
  316.         return NULL;
  317.     }
  318.     while ((ep = readdir(dirp)) != NULL) {
  319.         v = newstringobject(ep->d_name);
  320.         if (v == NULL) {
  321.             DECREF(d);
  322.             d = NULL;
  323.             break;
  324.         }
  325.         if (addlistitem(d, v) != 0) {
  326.             DECREF(v);
  327.             DECREF(d);
  328.             d = NULL;
  329.             break;
  330.         }
  331.         DECREF(v);
  332.     }
  333.     closedir(dirp);
  334.     END_SAVE
  335.  
  336.     return d;
  337. }
  338.  
  339. static object *
  340. mac_lseek(self, args)
  341.     object *self;
  342.     object *args;
  343. {
  344.     int fd;
  345.     int where;
  346.     int how;
  347.     long res;
  348.     if (!getargs(args, "(iii)", &fd, &where, &how))
  349.         return NULL;
  350.     BGN_SAVE
  351.     res = lseek(fd, (long)where, how);
  352.     END_SAVE
  353.     if (res < 0)
  354.         return mac_error();
  355.     return newintobject(res);
  356. }
  357.  
  358. static object *
  359. mac_mkdir(self, args)
  360.     object *self;
  361.     object *args;
  362. {
  363.     int res;
  364.     char *path;
  365.     int mode = 0777; /* Unused */
  366.     if (!newgetargs(args, "s|i", &path, &mode))
  367.         return NULL;
  368.     BGN_SAVE
  369. #ifdef USE_GUSI
  370.     res = mkdir(path);
  371. #else
  372.     res = mkdir(path, mode);
  373. #endif
  374.     END_SAVE
  375.     if (res < 0)
  376.         return mac_error();
  377.     INCREF(None);
  378.     return None;
  379. }
  380.  
  381. static object *
  382. mac_open(self, args)
  383.     object *self;
  384.     object *args;
  385. {
  386.     char *path;
  387.     int mode;
  388.     int fd;
  389.     if (!getargs(args, "(si)", &path, &mode))
  390.         return NULL;
  391.     BGN_SAVE
  392.     fd = open(path, mode);
  393.     END_SAVE
  394.     if (fd < 0)
  395.         return mac_error();
  396.     return newintobject((long)fd);
  397. }
  398.  
  399. static object *
  400. mac_read(self, args)
  401.     object *self;
  402.     object *args;
  403. {
  404.     int fd, size;
  405.     object *buffer;
  406.     if (!getargs(args, "(ii)", &fd, &size))
  407.         return NULL;
  408.     buffer = newsizedstringobject((char *)NULL, size);
  409.     if (buffer == NULL)
  410.         return NULL;
  411.     BGN_SAVE
  412.     size = read(fd, getstringvalue(buffer), size);
  413.     END_SAVE
  414.     if (size < 0) {
  415.         DECREF(buffer);
  416.         return mac_error();
  417.     }
  418.     resizestring(&buffer, size);
  419.     return buffer;
  420. }
  421.  
  422. static object *
  423. mac_rename(self, args)
  424.     object *self;
  425.     object *args;
  426. {
  427.     return mac_2str(args, rename);
  428. }
  429.  
  430. static object *
  431. mac_rmdir(self, args)
  432.     object *self;
  433.     object *args;
  434. {
  435.     return mac_1str(args, rmdir);
  436. }
  437.  
  438. static object *
  439. mac_stat(self, args)
  440.     object *self;
  441.     object *args;
  442. {
  443.     struct stat st;
  444.     char *path;
  445.     int res;
  446.     if (!getargs(args, "s", &path))
  447.         return NULL;
  448.     BGN_SAVE
  449.     res = stat(path, &st);
  450.     END_SAVE
  451.     if (res != 0)
  452.         return mac_error();
  453. #if 1
  454.     return mkvalue("(lllllllddd)",
  455.             (long)st.st_mode,
  456.             (long)st.st_ino,
  457.             (long)st.st_dev,
  458.             (long)st.st_nlink,
  459.             (long)st.st_uid,
  460.             (long)st.st_gid,
  461.             (long)st.st_size,
  462.             (double)st.st_atime,
  463.             (double)st.st_mtime,
  464.             (double)st.st_ctime);
  465. #else
  466.     return mkvalue("(llllllllll)",
  467.             (long)st.st_mode,
  468.             (long)st.st_ino,
  469.             (long)st.st_dev,
  470.             (long)st.st_nlink,
  471.             (long)st.st_uid,
  472.             (long)st.st_gid,
  473.             (long)st.st_size,
  474.             (long)st.st_atime,
  475.             (long)st.st_mtime,
  476.             (long)st.st_ctime);
  477. #endif
  478. }
  479.  
  480. static object *
  481. mac_xstat(self, args)
  482.     object *self;
  483.     object *args;
  484. {
  485.     struct macstat mst;
  486.     struct stat st;
  487.     char *path;
  488.     int res;
  489.     if (!getargs(args, "s", &path))
  490.         return NULL;
  491.     /*
  492.     ** Convoluted: we want stat() and xstat() to agree, so we call both
  493.     ** stat and macstat, and use the latter only for values not provided by
  494.     ** the former.
  495.     */
  496.     BGN_SAVE
  497.     res = macstat(path, &mst);
  498.     END_SAVE
  499.     if (res != 0)
  500.         return mac_error();
  501.     BGN_SAVE
  502.     res = stat(path, &st);
  503.     END_SAVE
  504.     if (res != 0)
  505.         return mac_error();
  506. #if 1
  507.     return mkvalue("(llllllldddls#s#)",
  508.             (long)st.st_mode,
  509.             (long)st.st_ino,
  510.             (long)st.st_dev,
  511.             (long)st.st_nlink,
  512.             (long)st.st_uid,
  513.             (long)st.st_gid,
  514.             (long)st.st_size,
  515.             (double)st.st_atime,
  516.             (double)st.st_mtime,
  517.             (double)st.st_ctime,
  518.             (long)mst.st_rsize,
  519.             mst.st_creator, 4,
  520.             mst.st_type, 4);
  521. #else
  522.     return mkvalue("(llllllllllls#s#)",
  523.             (long)st.st_mode,
  524.             (long)st.st_ino,
  525.             (long)st.st_dev,
  526.             (long)st.st_nlink,
  527.             (long)st.st_uid,
  528.             (long)st.st_gid,
  529.             (long)st.st_size,
  530.             (long)st.st_atime,
  531.             (long)st.st_mtime,
  532.             (long)st.st_ctime,
  533.             (long)mst.st_rsize,
  534.             mst.st_creator, 4,
  535.             mst.st_type, 4);
  536. #endif
  537. }
  538.  
  539. static object *
  540. mac_sync(self, args)
  541.     object *self;
  542.     object *args;
  543. {
  544.     int res;
  545.     if (!getnoarg(args))
  546.         return NULL;
  547.     BGN_SAVE
  548.     res = sync();
  549.     END_SAVE
  550.     if (res != 0)
  551.         return mac_error();
  552.     INCREF(None);
  553.     return None;
  554. }
  555.  
  556. static object *
  557. mac_unlink(self, args)
  558.     object *self;
  559.     object *args;
  560. {
  561.     return mac_1str(args, (int (*)(const char *))unlink);
  562. }
  563.  
  564. static object *
  565. mac_write(self, args)
  566.     object *self;
  567.     object *args;
  568. {
  569.     int fd, size;
  570.     char *buffer;
  571.     if (!getargs(args, "(is#)", &fd, &buffer, &size))
  572.         return NULL;
  573.     BGN_SAVE
  574.     size = write(fd, buffer, size);
  575.     END_SAVE
  576.     if (size < 0)
  577.         return mac_error();
  578.     return newintobject((long)size);
  579. }
  580.  
  581. #ifdef USE_MALLOC_DEBUG
  582. static object *
  583. mac_mstats(self, args)
  584.     object*self;
  585.     object *args;
  586. {
  587.     mstats("python");
  588.     INCREF(None);
  589.     return None;
  590. }
  591. #endif USE_MALLOC_DEBUG
  592.  
  593. static struct methodlist mac_methods[] = {
  594.     {"chdir",    mac_chdir},
  595.     {"close",    mac_close},
  596. #ifdef WEHAVE_DUP
  597.     {"dup",        mac_dup},
  598. #endif
  599. #ifdef WEHAVE_FDOPEN
  600.     {"fdopen",    mac_fdopen},
  601. #endif
  602.     {"getbootvol",    mac_getbootvol}, /* non-standard */
  603.     {"getcwd",    mac_getcwd},
  604.     {"listdir",    mac_listdir, 0},
  605.     {"lseek",    mac_lseek},
  606.     {"mkdir",    mac_mkdir, 1},
  607.     {"open",    mac_open},
  608.     {"read",    mac_read},
  609.     {"rename",    mac_rename},
  610.     {"rmdir",    mac_rmdir},
  611.     {"stat",    mac_stat},
  612.     {"xstat",    mac_xstat},
  613.     {"sync",    mac_sync},
  614.     {"remove",    mac_unlink},
  615.     {"unlink",    mac_unlink},
  616.     {"write",    mac_write},
  617. #ifdef USE_MALLOC_DEBUG
  618.     {"mstats",    mac_mstats},
  619. #endif
  620.  
  621.     {NULL,        NULL}         /* Sentinel */
  622. };
  623.  
  624.  
  625. void
  626. initmac()
  627. {
  628.     object *m, *d;
  629.     
  630.     m = initmodule("mac", mac_methods);
  631.     d = getmoduledict(m);
  632.     
  633.     /* Initialize mac.error exception */
  634.     MacError = newstringobject("mac.error");
  635.     if (MacError == NULL || dictinsert(d, "error", MacError) != 0)
  636.         fatal("can't define mac.error");
  637. }
  638.